/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.coupon.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.log.annotation.SysLog;
import com.ykht.proj.coupon.Enum.RewardPersonEnum;
import com.ykht.proj.coupon.entity.FxPersonFy;
import com.ykht.proj.coupon.entity.FxRewardPerson;
import com.ykht.proj.coupon.service.FxPersonFyService;
import com.ykht.proj.coupon.vo.FxPersonFyVo;
import com.ykht.proj.coupon.vo.FxPersonFyVo4PC;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;


/**
 * C端的饭友表
 *
 * @author csg
 * @date 2021-08-14 13:41:01
 */
@RestController
@AllArgsConstructor
@RequestMapping("/fxpersonfy" )
@Api(value = "fxpersonfy", tags = "饭友表管理")
@Slf4j
public class FxPersonFyController {

    private final  FxPersonFyService fxPersonFyService;

    /**
     * 分页查询 C端的饭友表
     * @param page 分页对象
     * @param fxPersonFyVo C端的饭友表
     * @return
     */
    @ApiOperation(value = "分页查询 C端的饭友表", notes = "分页查询 C端的饭友表")
    @GetMapping("/page" )
    public R<FxPersonFyVo4PC> getFxPersonFyPage(Page page, FxPersonFyVo fxPersonFyVo) {
		log.info("->分页查询 C端的饭友表 :{}", page, fxPersonFyVo);
		FxPersonFyVo4PC pc = new FxPersonFyVo4PC();
		IPage<FxPersonFyVo> iPage = fxPersonFyService.getFxPersonFyPage(page, fxPersonFyVo);
		pc.setPage(iPage);
		pc.setFyNum(iPage.getTotal());
		List<FxRewardPerson> personList = new FxRewardPerson().selectList(Wrappers.lambdaQuery(FxRewardPerson.class)
				.select(FxRewardPerson::getStatus,FxRewardPerson::getMoney)
				.in(FxRewardPerson::getStatus,RewardPersonEnum.not_push.getValue(),RewardPersonEnum.pushed.getValue())
				.eq(FxRewardPerson::getPersonId, fxPersonFyVo.getPerId()));
		personList.stream().filter(o->o.getStatus()== RewardPersonEnum.pushed.getValue()).map(FxRewardPerson::getMoney).reduce(BigDecimal::add).ifPresent(o->pc.setUseMoney(o));
		pc.setUseNum(personList.size());
        return R.ok(pc);
    }


    /**
     * 通过id查询C端的饭友表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('coupon_fxpersonfy_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(fxPersonFyService.getById(id));
    }

    /**
     * 新增C端的饭友表
     * @param fxPersonFy C端的饭友表
     * @return R
     */
    @ApiOperation(value = "新增C端的饭友表", notes = "新增C端的饭友表")
    @SysLog("新增C端的饭友表" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('coupon_fxpersonfy_add')" )
    public R save(@RequestBody FxPersonFy fxPersonFy) {
        return R.ok(fxPersonFyService.save(fxPersonFy));
    }

    /**
     * 修改C端的饭友表
     * @param fxPersonFy C端的饭友表
     * @return R
     */
    @ApiOperation(value = "修改C端的饭友表", notes = "修改C端的饭友表")
    @SysLog("修改C端的饭友表" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('coupon_fxpersonfy_edit')" )
    public R updateById(@RequestBody FxPersonFy fxPersonFy) {
        return R.ok(fxPersonFyService.updateById(fxPersonFy));
    }

    /**
     * 通过id删除C端的饭友表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除C端的饭友表", notes = "通过id删除C端的饭友表")
    @SysLog("通过id删除C端的饭友表" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('coupon_fxpersonfy_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(fxPersonFyService.removeById(id));
    }

}
